/*	$Id: thread.h 1118 2010-12-12 21:05:32Z phrakt $	*/
/*
 * Copyright (c) 2010 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


#ifndef _CANOPY_THREAD_H_
#define _CANOPY_THREAD_H_

#include <sys/types.h>

#include <pthread.h>

#include <canopy/cdefs.h>



#define CNP_THR_MAXNAMELEN		32

#define CNP_THRPOOL_MAXNAMELEN		32



#define CNP_THRPOOL_DETACH		0x0001



/* thread states */
enum cnp_thr_state {
	CNP_THREAD_INIT,		/* initializing */
	CNP_THREAD_WAIT,		/* waiting for connection */
	CNP_THREAD_BUSY,		/* busy servicing request */
	CNP_THREAD_DEAD,		/* exiting or dead */
};


#define CNP_MUTEX_INITIALIZER		{ PTHREAD_MUTEX_INITIALIZER }
#define CNP_RWLOCK_INITIALIZER		{ PTHREAD_RWLOCK_INITIALIZER }
#define CNP_COND_INITIALIZER		{ PTHREAD_COND_INITIALIZER }


struct cnp_thrkey {
	pthread_key_t     ct_key;
};



struct cnp_mutex {
	pthread_mutex_t   cm_mutex;
};



struct cnp_rwlock {
	pthread_rwlock_t  cr_rwlock;
};



struct cnp_cond {
	pthread_cond_t    cc_cond;
};



typedef struct cnp_mutex       cnp_mutex_t;
typedef struct cnp_rwlock      cnp_rwlock_t;
typedef struct cnp_cond        cnp_cond_t;
typedef struct cnp_thread      cnp_thr_t;
typedef struct cnp_thread_pool cnp_thrpool_t;
typedef struct cnp_thrkey      cnp_thrkey_t;




cnp_thrpool_t* cnp_thrpool_create  (const char *, int, sigset_t,
					void * (*)(void *), void *);
void           cnp_thrpool_destroy (cnp_thrpool_t *);
int            cnp_thrpool_getmin  (cnp_thrpool_t *);
int            cnp_thrpool_sigmask (cnp_thrpool_t *, sigset_t *, sigset_t *);
int            cnp_thrpool_signal  (cnp_thrpool_t *, int);


cnp_thr_t*     cnp_thr_create      (const char *name, void * (*)(void *), void *);
int            cnp_thr_cancel      (cnp_thr_t *);
int            cnp_thr_detach      (cnp_thr_t *);
void           cnp_thr_exit        (void *);
ssize_t        cnp_thr_getname     (cnp_thr_t *, char *, size_t);
int            cnp_thr_join        (cnp_thr_t *, void **);
cnp_thr_t*     cnp_thr_self        (void);
int            cnp_thr_setname     (cnp_thr_t *, const char *, ...)
					ATTR_FORMAT(2, 3);
int            cnp_thr_signal      (cnp_thr_t *, int);
int            cnp_thr_sigmask     (int, const sigset_t *, sigset_t *);
int            cnp_thr_keycreate   (cnp_thrkey_t *, void (*)(void *));
int            cnp_thr_keydelete   (cnp_thrkey_t);
void*          cnp_thr_getspecific (cnp_thrkey_t);
int            cnp_thr_setspecific (cnp_thrkey_t, void *);

int            cnp_mutex_init      (cnp_mutex_t *, void *);
int            cnp_mutex_destroy   (cnp_mutex_t *);
int            cnp_mutex_lock      (cnp_mutex_t *);
int            cnp_mutex_trylock   (cnp_mutex_t *);
int            cnp_mutex_unlock    (cnp_mutex_t *);

int            cnp_rwlock_init     (cnp_rwlock_t *, void *);
int            cnp_rwlock_destroy  (cnp_rwlock_t *);
int            cnp_rwlock_rdlock   (cnp_rwlock_t *);
int            cnp_rwlock_wrlock   (cnp_rwlock_t *);
int            cnp_rwlock_unlock   (cnp_rwlock_t *);

int            cnp_cond_init       (cnp_cond_t *, void *);
int            cnp_cond_destroy    (cnp_cond_t *);
int            cnp_cond_broadcast  (cnp_cond_t *);
int            cnp_cond_signal     (cnp_cond_t *);
int            cnp_cond_timedwait  (cnp_cond_t *, cnp_mutex_t *,
					const struct timespec *);
int            cnp_cond_wait       (cnp_cond_t *, cnp_mutex_t *);


#endif /* _CANOPY_THREAD_H_ */
